package com.wanglei.ssm.controller;

import com.wanglei.ssm.domain.Orders;
import com.wanglei.ssm.service.OrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * 订单控制器
 *
 * @author wl
 */
@Controller
@RequestMapping("/orders")
public class OrdersController {

    /**
     * 订单服务
     */
    @Autowired
    private OrdersService ordersService;

    /**
     * 分页查询订单信息
     *
     * @param page     当前页码
     * @param pageSize 每页展示条数
     * @return 订单列表
     * @throws Exception e
     */
    @RequestMapping("findAll.do")
    public ModelAndView findAll(
            @RequestParam(name = "page", defaultValue = "1") Integer page,
            @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) throws Exception {
        // 分页查询订单信息
        List<Orders> ordersList = ordersService.findAllByPage(page, pageSize);
        ModelAndView mv = new ModelAndView();
        mv.setViewName("orders-list");
        mv.addObject("ordersList", ordersList);
        return mv;
    }

    /**
     * 根据id查询订单详情信息
     *
     * @param id 订单id
     * @return 订单详情页面
     */
    @RequestMapping("findById")
    public ModelAndView findById(String id) throws Exception {
        Orders orders = ordersService.findById(id);
        ModelAndView mv = new ModelAndView();
        mv.setViewName("orders-show");
        mv.addObject("orders", orders);
        return mv;
    }
}
